package com.example.medical.controller.admin;

import com.example.medical.entity.dto.AdminUpdateLeadershipDto;
import com.example.medical.entity.dto.UserGetLeadershipListDto;
import com.example.medical.service.LeadershipService;
import com.example.medical.utils.common.enums.Code;
import com.example.medical.utils.common.result.Result;
import com.example.medical.utils.config.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/admin/leadership")
public class AdminLeadershipController {

    @Autowired
    private LeadershipService leadershipService;

    @PostMapping("/get_leadership_list")
    public Result AdminGetLeadershipList(@RequestBody UserGetLeadershipListDto dto){
        return leadershipService.UserGetLeadershipList(dto);
    }

    @GetMapping("/get_leadership_detail")
    public Result AdminGetLeadershipDetail(@RequestParam("id") Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        return Result.okResult(leadershipService.getById(id));
    }

    @GetMapping("/remove_leadership")
    public Result AdminRemoveLeadership(@RequestParam("id")Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        boolean checkRemove = leadershipService.removeById(id);
        if (!checkRemove) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        return Result.okResult(Code.SUCCESS);
    }

    @GetMapping("/remove_leadership_arr")
    public Result AdminRemoveLeadershipArr(@RequestParam("ids")String[] ids){
        return leadershipService.AdminRemoveLeadershipArr(ids);
    }

    @GetMapping("/update_get_leadership")
    public Result AdminUpdateGetLeadership(@RequestParam("id")Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        return Result.okResult(leadershipService.getById(id));
    }

    @PostMapping("/update_leadership")
    public Result AdminUpdateLeadership(@RequestParam(value = "id")Long id,
                                        @RequestParam(value = "name",required = false)String name,
                                        @RequestParam(value = "nick",required = false)String nick,
                                        @RequestParam(value = "phone",required = false)Long phone,
                                        @RequestParam(value = "number",required = false)Long number,
                                        @RequestParam(value = "file",required = false)MultipartFile file,
                                        @RequestParam(value = "sex",required = false)Integer sex,
                                        @RequestParam(value = "email",required = false)String email) throws Exception {
        return leadershipService.AdminUpdateLeadership(id,name,nick,phone,number,file,sex,email);
    }

    @PostMapping("/save_leadership")
    public Result AdminSaveLeadership(@RequestParam(value = "name")String name,
                                      @RequestParam(value = "nick")String nick,
                                      @RequestParam(value = "phone")Long phone,
                                      @RequestParam(value = "number")Long number,
                                      @RequestParam(value = "file")MultipartFile file,
                                      @RequestParam(value = "sex")Integer sex,
                                      @RequestParam(value = "email")String email) throws Exception {
        return leadershipService.AdminSaveLeadership(name,nick,phone,number,file,sex,email);
    }

    @PostMapping("/update_leadership_password")
    public Result AdminUpdateLeadershipPassword(@RequestParam("id")Long id){
        return leadershipService.AdminUpdateLeadershipPassword(id);
    }

}
